//
//  THJFabuXieyiVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/11/20.
//  Copyright © 2018年 河南德艺. All rights reserved.
//

#import "THJFabuXieyiVC.h"

@interface THJFabuXieyiVC ()<UIWebViewDelegate>
{
    UIWebView * webview;
}
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation THJFabuXieyiVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setStateBarColor:SingleColor(whiteColor)];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self startWithSecond:5];
    
    webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, ScreenHeight - NavbarHeight)];
    webview.backgroundColor = SingleColor(whiteColor);
    [self.view addSubview:webview];
    NSURL * fileURL = [[NSBundle mainBundle] URLForResource:@"fabuXY.html" withExtension:nil];
    webview.delegate = self;
    NSURLRequest * request = [NSURLRequest requestWithURL:fileURL];
    [webview loadRequest:request];
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [self SVHUDShow];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self SVHUDHiden];
}
- (IBAction)sureBtnAction:(id)sender {
    [self pop];
    [self.delegate acceptXieyi];
}
//按钮开始倒计时
-(void)startWithSecond:(NSInteger)second{
    __weak typeof(self) weakself = self;
    __block NSInteger timeout=second; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                [weakself.sureBtn setTitle:@"我同意" forState:UIControlStateNormal];
                weakself.sureBtn.userInteractionEnabled = YES;
                
            });
            
        }else{
            int seconds = timeout % 60;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"____%@",strTime);
                weakself.sureBtn.titleLabel.text = strTime;
                [weakself.sureBtn setTitle:[NSString stringWithFormat:@"%@",strTime] forState:UIControlStateNormal];
                weakself.sureBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}


@end
